<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="意见类型" prop="feedbackType">
        <el-select v-model="queryParams.feedbackType" placeholder="请选择意见类型" clearable>
          <el-option label="功能建议" value="功能建议" />
          <el-option label="bug反馈" value="bug反馈" />
          <el-option label="界面优化" value="界面优化" />
          <el-option label="其他" value="其他" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable>
          <el-option label="未阅览" value="0" />
          <el-option label="已阅览" value="1" />
          <el-option label="已处理" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['diet:feedback:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="反馈ID" align="center" prop="feedbackId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="意见类型" align="center" prop="feedbackType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.feedbackType === '功能建议'" type="success">{{ scope.row.feedbackType }}</el-tag>
          <el-tag v-else-if="scope.row.feedbackType === 'bug反馈'" type="danger">{{ scope.row.feedbackType }}</el-tag>
          <el-tag v-else-if="scope.row.feedbackType === '界面优化'" type="warning">{{ scope.row.feedbackType }}</el-tag>
          <el-tag v-else>{{ scope.row.feedbackType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="反馈内容" align="center" prop="content" :show-overflow-tooltip="true" />
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" type="info">未阅览</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="warning">已阅览</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="success">已处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['diet:feedback:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['diet:feedback:edit']"
          >处理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['diet:feedback:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 处理反馈对话框 -->
    <el-dialog title="处理反馈" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择处理状态">
            <el-option label="已阅览" value="1" />
            <el-option label="已处理" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理备注" prop="handleRemark">
          <el-input v-model="form.handleRemark" type="textarea" :rows="4" placeholder="请输入处理备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 反馈详情对话框 -->
    <el-dialog title="反馈详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="反馈ID">{{ viewForm.feedbackId }}</el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ viewForm.userId }}</el-descriptions-item>
        <el-descriptions-item label="意见类型">{{ viewForm.feedbackType }}</el-descriptions-item>
        <el-descriptions-item label="处理状态">
          <el-tag v-if="viewForm.status === '0'" type="info">未阅览</el-tag>
          <el-tag v-else-if="viewForm.status === '1'" type="warning">已阅览</el-tag>
          <el-tag v-else-if="viewForm.status === '2'" type="success">已处理</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="反馈内容" :span="2">{{ viewForm.content }}</el-descriptions-item>
        <el-descriptions-item label="处理备注" :span="2">{{ viewForm.handleRemark || '暂无' }}</el-descriptions-item>
        <el-descriptions-item label="提交时间">{{ parseTime(viewForm.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="处理时间">{{ parseTime(viewForm.handleTime) || '暂无' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { getFeedbackList, handleFeedback, delFeedback } from "@/api/diet/user";

export default {
  name: "DietFeedback",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      feedbackList: [],
      title: "",
      open: false,
      viewOpen: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        feedbackType: null,
        status: null
      },
      form: {},
      viewForm: {},
      rules: {
        status: [
          { required: true, message: "处理状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getFeedbackList(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        feedbackId: null,
        status: null,
        handleRemark: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.feedbackId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleUpdate(row) {
      this.reset();
      this.form.feedbackId = row.feedbackId;
      this.form.status = row.status;
      this.form.handleRemark = row.handleRemark;
      this.open = true;
      this.title = "处理反馈";
    },
    handleView(row) {
      this.viewForm = row;
      this.viewOpen = true;
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          handleFeedback(this.form).then(response => {
            this.$modal.msgSuccess("处理成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    handleDelete(row) {
      const feedbackIds = row.feedbackId || this.ids;
      this.$modal.confirm('是否确认删除反馈编号为"' + feedbackIds + '"的数据项？').then(function() {
        return delFeedback(feedbackIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>